//引入中间件
const jwt = require('jwt-simple')

// const adminName = 'admin'
// const secreat = '666' //加盐

//jwt的封装
 const jwtEncode = (adminName,secret) => {
  return jwt.encode({
    adminName,
    //token有效时间(秒)
    exp: Date.now() / 1000 + 60*60,
  }, secret)
}
//token解码封装函数
const jwtDecode = (token, secret) => {
  //有可能会解码错误
  try {
    const { adminName } = jwt.decode(token, secret)
    return {
      code: 1,
      adminName
    }
  } catch (e) {
    return {
      code: 0,
    }
  }

}
//暴露出去
module.exports = {
  jwtEncode,
  jwtDecode
}
// const token =

//   console.log(token, 'token');

//对token解码
// const tokenResult =

//   console.log(tokenResult);